package com.awcall.iot.controller;


import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.model.Statements;
import com.awcall.iot.model.User;
import com.awcall.iot.service.IStatementsService;
import com.awcall.iot.service.IUserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.awcall.iot.common.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhui
 * @since 2018-03-21
 */
@RestController
@RequestMapping("/admin/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IStatementsService iStatementsService;

    @GetMapping("/index")
    public ResponseResult index(User user, Page<User> page){

        EntityWrapper<User> entityWrapper = new EntityWrapper<>();

        if (StringUtils.isEmpty(user.getName())){
            entityWrapper.like(User.NAME,user.getName());
        }

        Page<User> userPage = iUserService.selectPage(page, entityWrapper);

        for (User b : userPage.getRecords()){
            EntityWrapper<Statements> statementsEntityWrapper = new EntityWrapper<>();
            statementsEntityWrapper.eq("user_id",b.getId());
            statementsEntityWrapper.in("type",new Integer[]{1,2}).setSqlSelect("ifnull(sum(price), 0)");
            Double originTotalAmount = (Double) iStatementsService.selectObj(statementsEntityWrapper);
            b.setPrice(originTotalAmount);
        }

        return new ResponseResult<>(userPage);
    }

    @GetMapping("/list")
    public ResponseResult list(User user){

        EntityWrapper<User> entityWrapper = new EntityWrapper<>();

        if (StringUtils.isEmpty(user.getName())){
            entityWrapper.like(User.NAME,user.getName());
        }

        List<User> users = iUserService.selectList(entityWrapper);

        return new ResponseResult<>(users);
    }

    @GetMapping("/detail")
    public ResponseResult detail(User user){

        User user1 = user.selectById();

        return new ResponseResult<>(user1);
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody User user){

        boolean insert = user.insert();

        if (insert) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody User user){

        boolean updateById = user.updateById();

        if (updateById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody User user){

        boolean deleteById = user.deleteById();

        if (deleteById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }        
            
}
